package studio.banner.reference.controller;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.bind.annotation.*;
import studio.banner.reference.entity.Aggregate;
import studio.banner.reference.entity.Document;
import studio.banner.reference.util.ResultBean;
import studio.banner.reference.util.WordUtil;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName AggregateController
 * @Deacription TODO
 * @Author Baofeng.Wu
 * @Date 2019/10/30 20:07
 **/
@RequestMapping("/aggregate")
@RestController
public class AggregateController extends BaseController {


    /**
     * 插入新的文献集
     *
     * @param aggregate
     * @param session
     * @return
     */
    @PutMapping("/insert")
    public ResultBean insertAggregate(@RequestBody Aggregate aggregate, HttpSession session) {
        Integer userId = getUserIdBySession(session);
        aggregate.setUserId(userId);
        aggregate.setId(0);
        return new ResultBean(aggregateService.insertAggregate(aggregate));
    }


    /**
     * 删除文献
     *
     * @param aggregateId
     * @return
     */
    @DeleteMapping("/delete")
    public ResultBean removeAggregate(Integer aggregateId) {
        documentService.removeByAggregateId(aggregateId);
        return new ResultBean(aggregateService.removeById(aggregateId));
    }

    /**
     * 批量删除文献
     *
     * @param aggregateIds
     * @return
     */
    public ResultBean removeAggregates(List<Integer> aggregateIds) {
        return new ResultBean(aggregateService.removeByIds(aggregateIds));
    }

    /**
     * 更新文献集标题
     *
     * @param aggregateTitle
     * @param aggregateId
     * @return
     */
    @PutMapping("/update/title")
    public ResultBean updateAggregatesTitle(String aggregateTitle, Integer aggregateId) {
        return new ResultBean(aggregateService.updateAggregateTitle(aggregateTitle, aggregateId));
    }

    /**
     * 返回文献集列表
     *
     * @param session
     * @return
     */
    public ResultBean listAggregatesByUserId(HttpSession session) {
        Integer userId = getUserIdBySession(session);
        return new ResultBean(aggregateService.listAggregatesByUserId(userId));
    }

    /**
     * 文献集查找，关键词
     *
     * @param keyword
     * @return
     */
    public ResultBean searchAggregatesByTitle(String keyword, HttpSession session) {
        Integer userId = getUserIdBySession(session);
        return new ResultBean(aggregateService.listAggregatesByAggregateTitle(keyword, userId));
    }


    /**
     * 文献集查找（ID）
     *
     * @param aggregateId
     * @return
     */
    public ResultBean searchAggregatesById(Integer aggregateId) {
        return new ResultBean(aggregateService.getById(aggregateId));
    }


    /**
     * 下载文献，格式为[.doc]
     *
     * @param aggregateId
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/download")
    public void downloadWord(Integer aggregateId, HttpServletResponse response) throws IOException {
        WordUtil wordUtil = new WordUtil();
        String fileName = "测试";
        List<Document> documents = documentService.listByAggregateId(aggregateId);
        XWPFDocument doc = wordUtil.generateDoc(documents);
        wordUtil.downloadFile(response, fileName, doc);
    }
}
